﻿#region

using Vin.Extension.Framework.Login.Entities;
using Vin.Extension.Framework.Repository;

#endregion

namespace Vin.Extension.Framework.Login.Repository;

/// <summary>
/// 账号仓储
/// </summary>
public class SysAccountRepository : VinFrameworkBaseRepository<SysAccount>
{
    /// <summary>
    /// 根据账号查询id
    /// </summary>
    public virtual async Task<long?> QueryIdByAccount(string account)
    {
        return (await QueryByAccountAsync(account))?.Id;
    }

    /// <summary>
    /// 根据账号查询
    /// </summary>
    public virtual async Task<SysAccount?> QueryByAccountAsync(string account)
    {
        return await GetFirstAsync(m => m.Account == account);
    }

    /// <summary>
    /// 创建账号（账号，密码）
    /// </summary>
    public virtual async Task<long?> CreateAccount(string account, string password)
    {
        return (await SaveOrUpdateAsync(new SysAccount()
        {
            Account = account,
            Password = password,
        }, true))?.Id;
    }

    public virtual async Task<bool> CheckAccountExistsAsync(string account)
    {
        return await IsAnyAsync(m => m.Account == account);
    }

    public virtual async Task<bool> UpdateAccountAsync(SysAccount account)
    {
        return await SaveOrUpdateAsync(account) != null;
    }
}